package com.dragon.mmochat.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.serverListStatistics.MmochatPayRecord;
import com.google.gson.JsonParseException;

//列表服务器统计
public class ServerListStatistics {
	// 已注册的所有skyId:skyId-->0
	public static Map<Long, Byte> skyIdMap = new ConcurrentHashMap<Long, Byte>();

	// 今日登陆的skyId:skyId-->loginTime
	public static Map<Long, Integer> todayLoginSkyIdMap = new ConcurrentHashMap<Long, Integer>();

	public ServerListStatistics() {
		try {
			// 加载skyIdMap文件
			File file = new File("skyIdMap");
			if (file.exists() && file.isFile()) {
				BufferedReader reader = new BufferedReader(new FileReader(file));
				if (reader != null) {
					String str = reader.readLine();
					if (str != null) {
						String subs[] = str.split(",");
						for (String sub : subs) {
							Long skyId = Long.parseLong(sub);
							skyIdMap.put(skyId, (byte) 0);
						}
						System.out.println("加载skyIdMap文件成功,num="
								+ skyIdMap.size());
					}
					reader.close();
				}
			} else {
				System.out.println("加载skyIdMap文件失败!");
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 统计日期内SKYID用户充值金额累计排行，由高到低降序排列
	public static List<MmochatPayRecord> rankSkyIdPay(String startDateStr,
			String endDateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date startDate = null, endDate = null;
		try {
			startDate = sdf.parse(startDateStr);
			endDate = sdf.parse(endDateStr);
		} catch (ParseException e) {
			return null;
		}
		long start = startDate.getTime();
		long end = endDate.getTime();
		List<MmochatPayRecord> records = new ArrayList<MmochatPayRecord>();
		// 卡密
		List<MmochatPayRecord> list = MmochatMain.serverListDaoService
				.mmochat1_getDirectPayRecord(start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat2_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat3_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat4_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat5_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat6_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat7_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat8_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat9_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat10_getDirectPayRecord(
				start, end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		// K币
		list = MmochatMain.serverListDaoService.mmochat1_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat2_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat3_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat4_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat5_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat6_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat7_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat8_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat9_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}
		list = MmochatMain.serverListDaoService.mmochat10_getKBPayRecord(start,
				end);
		if (list != null) {
			records.addAll(list);
			list.clear();
		}

		// 排序:skyId-->MmochatPayRecord
		Map<Long, MmochatPayRecord> payMap = new HashMap<Long, MmochatPayRecord>();
		for (MmochatPayRecord record : records) {
			if (record != null) {
				MmochatPayRecord oldRecord = payMap.get(record.getSkyId());
				if (oldRecord == null) {
					payMap.put(record.getSkyId(), record);
				} else {
					oldRecord.addRmb(record.getRmb());
				}
			}
		}
		records.clear();
		records = new ArrayList<MmochatPayRecord>(payMap.values());
		Collections.sort(records);
		return records;
	}
}
